import 'package:flutter/material.dart';
import '../base_notifier.dart';
import 'state_builder.dart';

// 状态包装器 - 提供更多自定义选项
class StateWrapper<T> extends StatelessWidget {
  const StateWrapper({
    super.key,
    required this.state,
    required this.child,
    this.loadingBuilder,
    this.errorBuilder,
    this.emptyBuilder,
    this.idleBuilder,
    this.onRetry,
    this.backgroundColor,
    this.padding,
    this.showRefreshIndicator = true,
  });

  final BaseState<T> state;
  final Widget Function(T data) child;
  final Widget Function(BuildContext context)? loadingBuilder;
  final Widget Function(BuildContext context, String error)? errorBuilder;
  final Widget Function(BuildContext context)? emptyBuilder;
  final Widget Function(BuildContext context)? idleBuilder;
  final VoidCallback? onRetry;
  final Color? backgroundColor;
  final EdgeInsets? padding;
  final bool showRefreshIndicator;

  @override
  Widget build(BuildContext context) {
    Widget content = StateBuilder<T>(
      state: state,
      onSuccess: child,
      onLoading: loadingBuilder != null ? () => loadingBuilder!(context) : null,
      onError: errorBuilder != null ? (error) => errorBuilder!(context, error) : null,
      onEmpty: emptyBuilder != null ? () => emptyBuilder!(context) : null,
      onIdle: idleBuilder != null ? () => idleBuilder!(context) : null,
      showRefreshIndicator: showRefreshIndicator,
      errorRetryCallback: onRetry,
    );

    if (padding != null) {
      content = Padding(padding: padding!, child: content);
    }

    if (backgroundColor != null) {
      content = Container(
        color: backgroundColor,
        child: content,
      );
    }

    return content;
  }
}

// 页面级别的状态管理器
class PageStateManager<T> extends StatelessWidget {
  const PageStateManager({
    super.key,
    required this.state,
    required this.onSuccess,
    this.onRetry,
    this.title,
    this.actions,
    this.floatingActionButton,
    this.showAppBar = true,
    this.showRefreshButton = true,
  });

  final BaseState<T> state;
  final Widget Function(T data) onSuccess;
  final VoidCallback? onRetry;
  final String? title;
  final List<Widget>? actions;
  final Widget? floatingActionButton;
  final bool showAppBar;
  final bool showRefreshButton;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: showAppBar
          ? AppBar(
              title: title != null ? Text(title!) : null,
              actions: [
                if (showRefreshButton && onRetry != null)
                  IconButton(
                    onPressed: onRetry,
                    icon: const Icon(Icons.refresh),
                  ),
                ...?actions,
              ],
            )
          : null,
      body: StateBuilder<T>(
        state: state,
        onSuccess: onSuccess,
        errorRetryCallback: onRetry,
      ),
      floatingActionButton: floatingActionButton,
    );
  }
}

// 列表状态管理器 - 专门处理列表数据
class ListStateManager<T> extends StatelessWidget {
  const ListStateManager({
    super.key,
    required this.state,
    required this.itemBuilder,
    this.separatorBuilder,
    this.onRetry,
    this.onRefresh,
    this.padding,
    this.shrinkWrap = false,
    this.physics,
    this.emptyMessage = '暂无数据',
    this.emptyIcon = Icons.inbox,
  });

  final BaseState<List<T>> state;
  final Widget Function(BuildContext context, T item, int index) itemBuilder;
  final Widget Function(BuildContext context, int index)? separatorBuilder;
  final VoidCallback? onRetry;
  final Future<void> Function()? onRefresh;
  final EdgeInsets? padding;
  final bool shrinkWrap;
  final ScrollPhysics? physics;
  final String emptyMessage;
  final IconData emptyIcon;

  @override
  Widget build(BuildContext context) {
    Widget content = StateBuilder<List<T>>(
      state: state,
      onSuccess: (data) => _buildList(data),
      onEmpty: () => _buildEmptyWidget(),
      errorRetryCallback: onRetry,
    );

    if (onRefresh != null) {
      content = RefreshIndicator(
        onRefresh: onRefresh!,
        child: content,
      );
    }

    return content;
  }

  Widget _buildList(List<T> data) {
    if (separatorBuilder != null) {
      return ListView.separated(
        padding: padding,
        shrinkWrap: shrinkWrap,
        physics: physics,
        itemCount: data.length,
        itemBuilder: (context, index) => itemBuilder(context, data[index], index),
        separatorBuilder: separatorBuilder!,
      );
    }

    return ListView.builder(
      padding: padding,
      shrinkWrap: shrinkWrap,
      physics: physics,
      itemCount: data.length,
      itemBuilder: (context, index) => itemBuilder(context, data[index], index),
    );
  }

  Widget _buildEmptyWidget() {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          Icon(emptyIcon, size: 64, color: Colors.grey),
          const SizedBox(height: 16),
          Text(
            emptyMessage,
            style: const TextStyle(
              fontSize: 18,
              fontWeight: FontWeight.bold,
              color: Colors.grey,
            ),
          ),
        ],
      ),
    );
  }
}

// 卡片式状态展示器
class StateCard<T> extends StatelessWidget {
  const StateCard({
    super.key,
    required this.state,
    required this.onData,
    this.onRetry,
    this.title,
    this.margin,
    this.elevation = 2,
  });

  final BaseState<T> state;
  final Widget Function(T data) onData;
  final VoidCallback? onRetry;
  final String? title;
  final EdgeInsets? margin;
  final double elevation;

  @override
  Widget build(BuildContext context) {
    return Card(
      margin: margin,
      elevation: elevation,
      child: Padding(
        padding: const EdgeInsets.all(16),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            if (title != null) ...[
              Text(
                title!,
                style: Theme.of(context).textTheme.titleMedium,
              ),
              const SizedBox(height: 8),
            ],
            StateBuilder<T>(
              state: state,
              onSuccess: onData,
              errorRetryCallback: onRetry,
              showRefreshIndicator: false,
            ),
          ],
        ),
      ),
    );
  }
}